package com.linksfield.topup.controller.anoverview;

import com.alibaba.fastjson.JSON;
import com.linksfield.topup.config.dto.ResultDataDto;
import com.linksfield.topup.config.dto.StringOutputConstant;
import com.linksfield.topup.entity.interfaces.anoverview.admin.fixeddata.request.RequestAdminFixedData;
import com.linksfield.topup.entity.interfaces.anoverview.admin.fixeddata.response.ResponseAdminFixedData;
import com.linksfield.topup.entity.interfaces.anoverview.admin.orderdata.monthdata.request.RequestAdminMonthOrder;
import com.linksfield.topup.entity.interfaces.anoverview.admin.orderdata.monthdata.response.ResponseAdminMonthOrder;
import com.linksfield.topup.entity.interfaces.anoverview.admin.orderdata.yeardata.request.RequestAdminYearOrder;
import com.linksfield.topup.entity.interfaces.anoverview.admin.orderdata.yeardata.response.ResponseAdminYearOrder;
import com.linksfield.topup.entity.interfaces.anoverview.admin.periodcount.ask.request.RequestAskOrderCount;
import com.linksfield.topup.entity.interfaces.anoverview.admin.periodcount.ask.response.ResponseAskOrderCount;
import com.linksfield.topup.entity.interfaces.anoverview.admin.periodcount.defaultask.request.RequestDefaultOrderCount;
import com.linksfield.topup.entity.interfaces.anoverview.admin.periodcount.defaultask.response.ResponseDefaultOrderCount;
import com.linksfield.topup.entity.interfaces.anoverview.admin.periodmoney.request.RequestAskOrderMoney;
import com.linksfield.topup.entity.interfaces.anoverview.admin.periodmoney.response.ResponseAskOrderMoney;
import com.linksfield.topup.entity.interfaces.anoverview.admin.salesrank.request.RequestAdminSalesRank;
import com.linksfield.topup.entity.interfaces.anoverview.admin.salesrank.response.ResponseAdminSalesRank;
import com.linksfield.topup.entity.interfaces.anoverview.partner.orderlinechart.request.RequestOrderLineChart;
import com.linksfield.topup.entity.interfaces.anoverview.partner.orderlinechart.response.ResponseOrderLineChart;
import com.linksfield.topup.entity.interfaces.anoverview.partner.orderpastdata.request.RequestOrderPastData;
import com.linksfield.topup.entity.interfaces.anoverview.partner.orderpastdata.response.ResponseOrderPastData;
import com.linksfield.topup.entity.interfaces.anoverview.partner.orderviewdata.request.RequestOrderViewData;
import com.linksfield.topup.entity.interfaces.anoverview.partner.orderviewdata.response.ResponseOrderViewData;
import com.linksfield.topup.entity.interfaces.anoverview.partner.salesrank.request.RequestSalesRank;
import com.linksfield.topup.entity.interfaces.anoverview.partner.salesrank.response.ResponseSalesRank;
import com.linksfield.topup.entity.interfaces.anoverview.partner.serviceview.request.RequestServiceView;
import com.linksfield.topup.entity.interfaces.anoverview.partner.serviceview.response.ResponseServiceView;
import com.linksfield.topup.service.OverviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理员或者普通企业：
 * 用户登录系统默认为用户展示的概览界面
 *
 * @author: lm
 * @date: 2020-03-31 10:00
 */
@RestController
@Api(value = "概览")

public class OverviewController {

    @Autowired
    OverviewService viewService;

    private static Logger logger = Logger.getLogger(OverviewController.class);

    @PostMapping(value = "partnerServiceView", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：普通企业：套餐总览", response = ResponseServiceView.class)
    public ResultDataDto partnerServiceView(@ApiParam @RequestBody RequestServiceView request,
                                            @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【普通企业：套餐总览】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.partnerServiceView(request, token);
        }
    }


    @PostMapping(value = "partnerOrderData", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：普通企业：订单总览，固定数据", response = ResponseOrderViewData.class)
    public ResultDataDto partnerOrderViewData(@ApiParam @RequestBody RequestOrderViewData request,
                                              @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【普通企业：订单总览，固定数据】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.partnerOrderViewData(request, token);
        }
    }

    /**
     * 2020-07-27添加：此接口为最新使用，折线图和往期数据都在partnerOrderLineChart一个接口完成
     *
     * @param request
     * @param token
     * @return
     */
    @PostMapping(value = "partnerOrderLineChart", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：普通企业：订单总览,折线图数据", response = ResponseOrderLineChart.class)
    public ResultDataDto partnerOrderLineChart(@ApiParam @RequestBody RequestOrderLineChart request,
                                               @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【普通企业：订单总览，折线图数据】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.partnerOrderLineChart(request, token);
        }
    }

    /**
     * 2020-07-27修改：此接口已经不再使用，折线图和往期数据都在partnerOrderLineChart一个接口完成
     *
     * @param request
     * @param token
     * @return
     */
    @PostMapping(value = "partnerOrderView", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：普通企业：往期按月份数据数据一致", response = ResponseOrderPastData.class)
    public ResultDataDto partnerOrderView(@ApiParam @RequestBody RequestOrderPastData request,
                                          @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【普通企业：订单总览，往期按月份数据】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.partnerOrderView(request, token);
        }
    }

    /***
     * 重要！！！
     * 测试时套餐的名称还是packageName，正式上线之后要改成servicePlanShowName作为查询名称
     * @param request
     * @param token
     * @return
     */
    @PostMapping(value = "partnerSalesRank", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：普通企业：套餐销售排行榜Top20", response = ResponseSalesRank.class)
    public ResultDataDto partnerSalesRank(@ApiParam @RequestBody RequestSalesRank request,
                                          @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【普通企业：套餐销售排行榜Top20】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.partnerSalesRank(request, token);
        }
    }

    /**
     * 概览：管理员：周月订单，周月订单金额：顶部固定数据
     *
     * @param request
     * @param token
     * @return
     */
    @PostMapping(value = "adminFixedData", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：管理员：周月订单，周月订单金额：顶部固定数据", response = ResponseAdminFixedData.class)
    public ResultDataDto adminFixedData(@ApiParam @RequestBody RequestAdminFixedData request,
                                        @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【管理员：周月订单，周月订单金额：顶部固定数据】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.adminFixedData(request, token);
        }
    }


    @PostMapping(value = "adminSalesRank", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：管理员：企业销售排行榜Top20", response = ResponseAdminSalesRank.class)
    public ResultDataDto adminSalesRank(@ApiParam @RequestBody RequestAdminSalesRank request,
                                        @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【管理员：套餐销售排行榜Top20】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.adminSalesRank(request, token);
        }
    }


    @PostMapping(value = "adminWithMonthData", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：管理员：按月和企业查询", response = ResponseAdminMonthOrder.class)
    public ResultDataDto adminWithMonthData(@ApiParam @RequestBody RequestAdminMonthOrder request,
                                            @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【管理员：按月和企业查询】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.adminWithMonthData(request, token);
        }
    }


    @PostMapping(value = "adminWithYearData", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：管理员：按年和企业查询", response = ResponseAdminYearOrder.class)
    public ResultDataDto adminWithYearData(@ApiParam @RequestBody RequestAdminYearOrder request,
                                           @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【管理员：按年和企业查询】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.adminWithYearData(request, token);
        }
    }

    @PostMapping(value = "defaultOrderCount", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：管理员：默认请求：：：按月或者按年某企业给出某个套餐按规格订购的订单数", response = ResponseDefaultOrderCount.class)
    public ResultDataDto defaultOrderCount(@ApiParam @RequestBody RequestDefaultOrderCount request,
                                           @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【管理员：默认某个套餐按规格订购的订单数】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.defaultOrderCount(request, token);
        }
    }

    @PostMapping(value = "askOrderCount", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：管理员：实际请求：：：按月或者按年某企业给出某个套餐按规格订购的订单数", response = ResponseAskOrderCount.class)
    public ResultDataDto askOrderCount(@ApiParam @RequestBody RequestAskOrderCount request,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【管理员：实际请求按规格订购的订单数】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.askOrderCount(request, token);
        }
    }

    @PostMapping(value = "askOrderMoney", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "概览：管理员：实际请求：：：按月或者按年某企业给出某个套餐按规格订购的订单总金额", response = ResponseAskOrderMoney.class)
    public ResultDataDto askOrderMoney(@ApiParam @RequestBody RequestAskOrderMoney request,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【管理员：实际请求按规格订购的订单总金额】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        } else {
            return viewService.askOrderMoney(request, token);
        }
    }
}
